package com.atguigu.guli.infrastructure.apigateway.filter;

import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AuthGlobaFilter implements GlobalFilter, Ordered {
    //给路径中含有auth的路径加过滤器，判断是否已经登录
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest(); //webflux的，不是mvc，servlet类型的
        String path = request.getURI().getPath(); //端口号后面，？前面的地址
        AntPathMatcher antPathMatcher = new AntPathMatcher(); // 路径比较器
        if (antPathMatcher.match("/api/**/auth/**", path)) {
//            List<String> list = request.getHeaders().get("token");
//            if (list == null) {
//                ServerHttpResponse response = exchange.getResponse();
//                return out(response);
//            }
//            boolean b = JwtHelper.checkToken(list.get(0));
            String token = request.getHeaders().getFirst("token");
            if(StringUtils.isEmpty(token)){
                ServerHttpResponse response = exchange.getResponse();
                return out(response);
            }
            boolean b = JwtHelper.checkToken(token);
            if (!b) {
                ServerHttpResponse response = exchange.getResponse();
                return out(response);
            }
        }
        return chain.filter(exchange);
    }


    @Override
    public int getOrder() {
        return 0;
    }
    private Mono<Void> out(ServerHttpResponse response) {

//        JsonObject message = new JsonObject();
//        message.addProperty("success", ResultCodeEnum.LOGIN_AUTH.getSuccess());
//        message.addProperty("code", ResultCodeEnum.LOGIN_AUTH.getCode());
//        message.addProperty("data", "");
//        message.addProperty("message", "用户还未登录");
//        byte[] bytes = message.toString().getBytes(StandardCharsets.UTF_8);
        Map map = new HashMap();
        map.put("success", ResultCodeEnum.LOGIN_AUTH.getSuccess());
        map.put("code", ResultCodeEnum.LOGIN_AUTH.getCode());
        map.put("data", "");
        map.put("message", "用户未登录");
        String resString = new Gson().toJson(map);
        DataBuffer buffer = response.bufferFactory().wrap(resString.getBytes());
        //指定编码，否则在浏览器中会中文乱码
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        //输出http响应
        return response.writeWith(Mono.just(buffer));
    }
}
